﻿using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CallCenter.Data;
using CallCenter.Data.BizUtilities;
using System.Configuration;
using System.Data;
using CallCenter.Security;
using CallCenter.Data.Models;
using CallCenter.Data.Misc;


namespace CallCenter.Web.Controllers
{
    [AuthorizeCore]
    public class AddContactController : BaseController
    {
        // GET: AddContact
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult ContactHistory()
        {
            ContactGridViewModel cnt = new ContactGridViewModel();
            return View(cnt);
        }
        [HttpGet]
        public JsonResult GetAddContacts()
        {
            var scratchPads = this.Bridge.ccContactUtility.GetAll();
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetContactsByLoggedInUser()
        {
            if (User.Identity.IsAuthenticated)
            {
                var scratchPads = this.Bridge.ccContactUtility.GetAllContactByLoggedInUser(User.Identity.Name);
                return Json(scratchPads, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }


        public JsonResult GetContactTypes()
        {
            var scratchPads = this.Bridge.ContactTypeUtilitycs.GetContactTypes();
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetJobTitles()
        {
            var scratchPads = this.Bridge.ContactTypeUtilitycs.GetJobtitle();
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetDepartments()
        {
            var scratchPads = this.Bridge.ContactTypeUtilitycs.GetDepartments();
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetListOfUsers()
        {
            var scratchPads = this.Bridge.AccountUtility.GetListOfActiveUsers();
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult CreateAddContact(ContactGridViewModel model)
        {
            model.CreatedUser = User.Identity.Name;
            var returnMessage = this.Bridge.ccContactUtility.InsertToContact(model);
            if (returnMessage.Message == "ISPRESENT")
            {
                var _Contact = this.Bridge.ccContactUtility.GetContactDetailByNameandID(model);
                model.SavedResult += "<table>";
                foreach (var item in _Contact)
                {
                    model.SavedResult += "<tr>";
                    model.SavedResult += "<td style='font-weight: bolder;'> Contact Name </td><td>" + item.lastName + "," + item.firstName + "</td>";
                    string Company = string.IsNullOrEmpty(item.Company) ? "Company detail is not Mapped" : item.Company;
                    model.SavedResult += "<td style='font-weight: bolder;'> | Company </td><td>" + Company + "</td>";
                    model.SavedResult += "</tr>";
                }
                model.SavedResult += "</table>";
                model.SavedResult += "<div> The Saved Record is already present in our Database! Are you sure you want to Save with the Same?";
                model.SavedResult += "<input type='button' name='name' value='Yes' onclick='SavePresentContact(" + model.id + ")' />";
                model.SavedResult += "<input type='button' name='name' value='No' onclick='DeletePresentContact(" + model.id + ")' />";
                model.SavedResult += "</div>";
            }
            else
            {
                model.SavedResult = "CREATED";
            }
            //model.SavedResult += "<h5>";
            //model.SavedResult += returnMessage.Message;
            //model.SavedResult += "</h5>";
            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult DeleteAddContact(ContactGridViewModel model)
        {

            int id = model.id;
            this.Bridge.ccContactUtility.Delete(id);
            var returnMessage = "Success";
            return Json(returnMessage, JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        public JsonResult UpdateAddContact(ContactGridViewModel model)
        {
            var result = this.Bridge.ccContactUtility.UpdateToContact(model);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public string SaveIsPresentContact(int id)
        {
            var returnMessage = this.Bridge.ccContactUtility.SaveIsPresentContact(id);
            return returnMessage;
        }

        public string DeletePresentContact(int id)
        {
            var returnMessage = this.Bridge.ccContactUtility.DeletePresentContact(id);
            return returnMessage;
        }
    }
}